/*

  The MIT License

  Copyright (c) 2008 - 2011. Dimarik

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.

*/

package ru.goodcore.display.gfx.core
{

import flash.display.Sprite;
import flash.display.Stage;
import flash.events.Event;
import flash.events.IEventDispatcher;
import flash.geom.Point;

/**
 * 
 * 
 * @author				Dimarik
 * @version				1.2
 * @langversion			3.0
 * @playerversion		9.0
 * 
 */
public class UIComponent extends Sprite {

	//-------------------------------------------------------------------------
	//
	//   Class variables
	//
	//-------------------------------------------------------------------------
	/**
	 * @private
	 */
	private static var isStageIvalidated:	Boolean = false;

	//-------------------------------------------------------------------------
	//
	//   Class methods
	//
	//-------------------------------------------------------------------------

	private static function handler_renderStage(event:Event):void {
		trace('Reset isStageIvalidated flag');
		(event.target as IEventDispatcher).removeEventListener(event.type, arguments.callee);
		isStageIvalidated = false;
	}

	//-------------------------------------------------------------------------
	//
	//   Constructor
	//
	//-------------------------------------------------------------------------

	/**
	 * Constructor
	 */
	public function UIComponent() {
		super();
		super.addEventListener(Event.ADDED_TO_STAGE, this.handler_addedToStage, false, int.MAX_VALUE);
	}

	//-------------------------------------------------------------------------
	//
	//   Overridden properties: Sprite
	//
	//-------------------------------------------------------------------------

	//---------------------------------
	//  width
	//---------------------------------
	/**
	 * @private 
	 */
	//private var _width:			Number = 0;

	/**
	 * @inheritDoc
	 */
	public override function get width():Number {
		return this._dimension.x;
	}

	/**
	 * @inheritDoc
	 */
	public override function set width(value:Number):void {
		if (this._dimension.x === value)
			return;
		//this._width = value;
		//super.width = value;
		this._dimension.x = value;
		this.invalidate();
	}

	/**
	 * @inheritDoc
	 */
	public override function get height():Number {
		return this._dimension.y;
	}

	/**
	 * @inheritDoc
	 */
	public override function set height(value:Number):void {
		if (this._dimension.y === value)
			return;
		//this._width = value;
		//super.width = value;
		this._dimension.y = value;
		this.invalidate();
	}


	//---------------------------------
	//  explicitWidth
	//---------------------------------
	/**
	 * @private
	 */
	private var _explicitWidth:		Number = 0;

	protected function set explicitWidth(value:Number):void {
		this._explicitWidth = value;
	}

	protected function get explicitWidth():Number {
		return this._explicitWidth;
	}

	//---------------------------------
	//  explicitHeight
	//---------------------------------
	/**
	 * @private
	 */
	private var _explicitHeight:		Number = 0;

	public function set explicitHeight(value:Number):void {
		this._explicitHeight = value;
	}

	public function get explicitHeight():Number {
		return this._explicitHeight;
	}

	//-------------------------------------------------------------------------
	//
	//   Properties
	//
	//-------------------------------------------------------------------------

	//-------------------------------------------------------------------------
	//
	//   Private variables
	//
	//-------------------------------------------------------------------------

	private const _dimension:Point = new Point();

	protected function get dimension():Point {
		return this._dimension;
	}
	
	private var _isValid:		Boolean = false;

	//-------------------------------------------------------------------------
	//
	//   Overridden methods:
	//
	//-------------------------------------------------------------------------

	//-------------------------------------------------------------------------
	//
	//   Protected methods
	//
	//-------------------------------------------------------------------------

	protected function invalidate():void {
		trace('try invalidate', super.name);

		if ( super.hasEventListener(Event.RENDER) ) return;

		trace('invalidate', super.name);

		this._isValid = false;
		super.addEventListener(Event.RENDER, this.handler_render, false, int.MAX_VALUE);

		if ( !isStageIvalidated ) {
			const stage:Stage = super.stage;
			if (stage) {
				isStageIvalidated = true;
				stage.addEventListener(Event.RENDER, handler_renderStage, false, int.MIN_VALUE, true);
				stage.invalidate();
			}
		}
	}

	protected function validate():void {
		trace('validate', super.name);
		this._isValid = true;
	}

	//-------------------------------------------------------------------------
	//
	//   Private methods
	//
	//-------------------------------------------------------------------------

	//-------------------------------------------------------------------------
	//
	//   Event handlers
	//
	//-------------------------------------------------------------------------

	/**
	 * @private
	 */
	private function handler_render(event:Event):void {
		trace('handler_render', super.name);
		super.removeEventListener(event.type, arguments.callee);
		this.validate();
	}

	/**
	 * @private
	 */
	private function handler_addedToStage(event:Event):void {
		trace('handler_addedToStage', super.name);
		if ( !this._isValid ) {
			this.invalidate();
		}
	}

}
}
